import { createRouter, createWebHistory } from 'vue-router';

// 认证模块
import Login from '../views/auth/Login.vue';
import Register from '../views/auth/Register.vue';

// 首页模块
import Home from '../views/home/Home.vue';

// 二手商城模块
import ItemList from '../views/shop/ItemList.vue';
import ItemDetail from '../views/shop/ItemDetail.vue';
import PublishItem from '../views/shop/PublishItem.vue';
import MyItems from '../views/shop/MyItems.vue';

// 订单模块
import OrderList from '../views/order/OrderList.vue';
import OrderDetail from '../views/order/OrderDetail.vue';
import OrderPay from '../views/order/OrderPay.vue';

// 环保模块
import BehaviorRecord from '../views/eco/BehaviorRecord.vue';
import CarbonTrends from '../views/eco/CarbonTrends.vue';
import Ranking from '../views/eco/Ranking.vue';

// 垃圾分类模块
import GarbageClassify from '../views/garbage/GarbageClassify.vue';
import ClassifyHistory from '../views/garbage/ClassifyHistory.vue';

// AI智能体模块
import EcoQA from '../views/ai/EcoQA.vue';
import PlanGenerate from '../views/ai/PlanGenerate.vue';
import PlanDetail from '../views/ai/PlanDetail.vue';
import PlanFeedback from '../views/ai/PlanFeedback.vue';

// 数据可视化模块
import EnvironmentData from '../views/visualization/EnvironmentData.vue';
import PersonalContribution from '../views/visualization/PersonalContribution.vue';
import DataDashboard from '../views/visualization/DataDashboard.vue';

// 用户中心模块
import Profile from '../views/user/Profile.vue';
import Settings from '../views/user/Settings.vue';
import CarbonPoints from '../views/user/CarbonPoints.vue';

const routes = [
  // 认证
  { path: '/login', name: 'Login', component: Login, meta: { requiresAuth: false } },
  { path: '/register', name: 'Register', component: Register, meta: { requiresAuth: false } },
  
  // 首页
  { path: '/', name: 'Home', component: Home, meta: { requiresAuth: true } },
  
  // 二手商城
  { path: '/shop/items', name: 'ItemList', component: ItemList, meta: { requiresAuth: true } },
  { path: '/shop/detail/:id', name: 'ItemDetail', component: ItemDetail, meta: { requiresAuth: true } },
  { path: '/shop/publish', name: 'PublishItem', component: PublishItem, meta: { requiresAuth: true } },
  { path: '/shop/my-items', name: 'MyItems', component: MyItems, meta: { requiresAuth: true } },
  
  // 订单
  { path: '/order/list', name: 'OrderList', component: OrderList, meta: { requiresAuth: true } },
  { path: '/order/:orderNo', name: 'OrderDetail', component: OrderDetail, meta: { requiresAuth: true } },
  { path: '/order/pay/:orderNo', name: 'OrderPay', component: OrderPay, meta: { requiresAuth: true } },
  
  // 环保模块
  { path: '/eco/record', name: 'BehaviorRecord', component: BehaviorRecord, meta: { requiresAuth: true } },
  { path: '/eco/trends', name: 'CarbonTrends', component: CarbonTrends, meta: { requiresAuth: true } },
  { path: '/eco/ranking', name: 'Ranking', component: Ranking, meta: { requiresAuth: true } },
  
  // 垃圾分类
  { path: '/garbage/classify', name: 'GarbageClassify', component: GarbageClassify, meta: { requiresAuth: true } },
  { path: '/garbage/history', name: 'ClassifyHistory', component: ClassifyHistory, meta: { requiresAuth: true } },
  
  // AI智能体
  { path: '/ai/qa', name: 'EcoQA', component: EcoQA, meta: { requiresAuth: true } },
  { path: '/ai/plan/generate', name: 'PlanGenerate', component: PlanGenerate, meta: { requiresAuth: true } },
  { path: '/ai/plan/:id', name: 'PlanDetail', component: PlanDetail, meta: { requiresAuth: true } },
  { path: '/ai/plan/:id/feedback', name: 'PlanFeedback', component: PlanFeedback, meta: { requiresAuth: true } },
  
  // 数据可视化
  { path: '/viz/environment', name: 'EnvironmentData', component: EnvironmentData, meta: { requiresAuth: true } },
  { path: '/viz/personal', name: 'PersonalContribution', component: PersonalContribution, meta: { requiresAuth: true } },
  { path: '/viz/dashboard', name: 'DataDashboard', component: DataDashboard, meta: { requiresAuth: true }},

// 用户中心
{
        path: '/user/profile', 
        name: 'Profile', 
        component: Profile, 
        meta: { requiresAuth: true } 
    },
    {
         path: '/user/settings', 
        name: 'Settings', 
        component: Settings, 
        meta: { requiresAuth: true } 
    },
    {
         path: '/user/carbon-points', 
        name: 'CarbonPoints', 
        component: CarbonPoints, 
        meta: { requiresAuth: true } 
    }
];   
// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes
});

// 路由守卫
router.beforeEach((to, _from, next) => {
  const token = localStorage.getItem('token');
  
  // 如果需要认证但没有token，跳转到登录页
  if (to.meta.requiresAuth && !token) {
    next('/login');
    return;
  } 
  // 如果已登录访问登录页或注册页，跳转到首页
  if ((to.path === '/login' || to.path === '/register') && token) {
    next('/');
    return;
  } 
  
  next();
});

export default router;
